package org.jeecg.eqp.controller;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.eqp.entity.MesPallet;
import org.jeecg.eqp.service.IMesPalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "托盘主数据")
@RestController
@RequestMapping("/eqp/pallet")
@Slf4j
public class MesPalletController extends JeecgController<MesPallet, IMesPalletService> {

	@Autowired
	private IMesPalletService mesPalletService;
	
	/**
	 * 单表分页列表查询
	 *
	 * @param eqpEqpchkCfg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "托盘主数据-分页列表查询", notes = "托盘主数据-分页列表查询")
	@GetMapping(value = "/list")
	@DS("history")
	public Result<IPage<MesPallet>> queryPageList(MesPallet mesPallet,
													 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
													 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
													 HttpServletRequest req) {
		QueryWrapper<MesPallet> queryWrapper = QueryGenerator.initQueryWrapper(mesPallet, req.getParameterMap());
		Page<MesPallet> page = new Page<MesPallet>(pageNo, pageSize);
		IPage<MesPallet> pageList = mesPalletService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
     * 添加
     *
     * @param mesPallet
     * @return
     */
    @AutoLog(value = "托盘主数据-添加")
    @ApiOperation(value = "托盘主数据-添加", notes = "托盘主数据-添加")
    @PostMapping(value = "/add")
    @DS("business")
    public Result<String> add(@RequestBody MesPallet mesPallet) throws Exception {
    	mesPallet.setCtime(new Date());
    	mesPalletService.save(mesPallet);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eqpEqpchkCfg
     * @return
     */
    @AutoLog(value = "托盘主数据-编辑")
    @ApiOperation(value = "托盘主数据-编辑", notes = "托盘主数据-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("business")
    public Result<String> edit(@RequestBody MesPallet mesPallet) throws Exception {
        //框架修改 eqpEqpchkCfgService.updateById(eqpEqpchkCfg);
        //return Result.OK("编辑成功!");
    	mesPallet.setCtime(new Date());
    	mesPalletService.updateById(mesPallet);
        return Result.OK("修改成功！");
    }

    /**
     * 通过map删除
     *
     * @param map
     * @return
     */
    @AutoLog(value = "托盘主数据-通过id删除")
    @ApiOperation(value = "托盘主数据-通过id删除", notes = "托盘主数据-通过id删除")
    @DeleteMapping(value = "/delete")
    @DS("business")
    public Result<String> delete(MesPallet mesPallet) throws Exception {
    	mesPalletService.removeById(mesPallet);
        return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "托盘主数据-批量删除")
	@ApiOperation(value = "托盘主数据-批量删除", notes = "托盘主数据-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@DS("business")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.mesPalletService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "托盘主数据-通过id查询")
	@ApiOperation(value = "托盘主数据-通过id查询", notes = "托盘主数据-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("business")
	public Result<MesPallet> queryById(@RequestParam(name = "id", required = true) String id) {
		MesPallet mesPallet = mesPalletService.getById(id);
		if (mesPallet == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mesPallet);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param eqpEqpchkCfg
	 */
	@RequestMapping(value = "/exportXls")
	@DS("history")
	public ModelAndView exportXls(HttpServletRequest request, MesPallet mesPallet) {
		return super.exportXls(request, mesPallet, MesPallet.class, "托盘主数据");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@DS("business")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, MesPallet.class);
	}
}
